package com.lighthouse.base.content.service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import com.lighthouse.base.common.dao.generalDAO;
import com.lighthouse.base.content.vo.ContentVO;
import com.lighthouse.base.content.vo.SearchContentVO;


@Service("ContentService")
public class ContentServiceImpl implements ContentService{
	protected Log log = LogFactory.getLog(this.getClass());
	
    @Resource (name = "generalDao")
    generalDAO generalDao;

    static private int VALIDATE_SUCCESS = 0;
    static private int VALIDATE_NOT_DEFILE_SEARCH_CODITION = -1;
    
    private int getValidateSearchCondition(SearchContentVO searchVo) {
    	return VALIDATE_SUCCESS;
    }
    
	public int getCount(SearchContentVO searchVo) {
		int nTotalCount = -1, nValidateCheckCode=VALIDATE_SUCCESS;
		try {
			if(searchVo==null) {
				nTotalCount = -1;
				if(log.isDebugEnabled()) log.debug(String.format("%s:SearchContentVO is null", this.getClass().getName()));
			}
			else {
				//Check Non Search Condition
				if((nValidateCheckCode=getValidateSearchCondition(searchVo))==VALIDATE_SUCCESS) {
					nTotalCount = generalDao.getCount("contentmanager_getcount", searchVo);
					if(log.isDebugEnabled()) log.debug(String.format("%s, TotalCount:%d", this.getClass().getName(), nTotalCount));
				}
				else {
					nTotalCount = -1;
					if(log.isDebugEnabled()) log.debug(String.format("%s, Invalidate Search Condition:%d", this.getClass().getName(), nValidateCheckCode));
				}
			}
		}
		catch(Exception e){
			if(log.isDebugEnabled()) log.debug(String.format("[%s]Exception:%s", this.getClass().getName(), e.toString()));
		}
		finally {
			//
		}
		return nTotalCount;
	}
	
	public List<ContentVO> getContentList(SearchContentVO searchVo) {
		int nValidateCheckCode = VALIDATE_SUCCESS;
		List<ContentVO> contentVoList = new ArrayList<ContentVO>();
	
		try {
			//check validate
			if((nValidateCheckCode=getValidateSearchCondition(searchVo))==VALIDATE_SUCCESS) {
				contentVoList = generalDao.select("contentmanager_getlist", searchVo);
			}
			else {
				contentVoList = null;
				if(log.isDebugEnabled()) log.debug(String.format("%s, Invalidate Search Condition:%d", this.getClass().getName(), nValidateCheckCode));
			}
		
		}
		catch(Exception e){
			if(log.isDebugEnabled()) log.debug(String.format("[getContentList]Exception:%s", e.toString()));
			contentVoList = null;
		}
		finally {
			//
		}
		return contentVoList;
	}
	
	public ContentVO getContentInfo(SearchContentVO searchVo) {
		List<ContentVO> contentVoList = new ArrayList<ContentVO>();
		ContentVO contentVo = null;
		try {
			if((searchVo.getContentId()!=null)&&(searchVo.getContentId()>0)) {
				//ID를 복수로 지정하여 검색 결과가 복수개 일수 있음
				contentVoList = generalDao.select("contentmanager_getlist", searchVo);
				if((contentVoList!=null)&&(contentVoList.size()>0)) contentVo = contentVoList.get(0);
			}
			else {
				if(log.isDebugEnabled()) log.debug(String.format("%s, Invalidate contentId", this.getClass().getName()));
			}
		}
		catch(Exception e){
			if(log.isDebugEnabled()) log.debug(String.format("[getContentInfo]Exception:%s", e.toString()));
			contentVo = null;
		}
		finally {
			//
		}
		return contentVo;
	}
}
